*     *****************************************************************   *;
*     *****************************************************************   *; 
*   Date:          December 27, 2021                               	      *;
*   Authors:       Lisa Dellmuth and Jonas Tallberg				          *;   
*   Purpose: 	   Analyses for book Legitimacy Politics: Elite  		  *;
*    Communication and Public Opinion in Global Governance (CUP, 2023)    *;
*     ****************************************************************    *;
*     ****************************************************************    *;



*Latinobarometro data collection for:
* individual-level data for each of the regional organizations (Mercosur and Adean Pact) for all the countries and years available
* data structure: individuals clustered in countries clustered in years; and the trust/support variables in the columns. 

 *2001
 clear
 use Latinobarometro_2001_datos_english_v2014_06_27.dta

 br idenpa numinves p67nba p67nbd
 
 rename numinves year
 rename idenpa country
 rename p67nba mercosur_trust
 rename p67nbd andeanpact_trust
 rename p67nbe imf_trust
 rename p67nbf worldbank_trust

  
 tab mercosur_trust 
 tab mercosur_trust ,nol
 tab andeanpact_trust , nol
 tab imf_trust, nol 
 tab worldbank_trust, nol
 
 replace mercosur_trust =. if mercosur_trust== -2 
 replace mercosur_trust =. if mercosur_trust== -3 
 replace mercosur_trust =. if mercosur_trust== -4
 
 replace andeanpact_trust =. if andeanpact_trust== -2 
 replace andeanpact_trust =. if andeanpact_trust== -3 
 replace andeanpact_trust =. if andeanpact_trust== -4

 replace imf_trust =. if imf_trust== -2 
 replace imf_trust =. if imf_trust== -3 
 replace imf_trust =. if imf_trust== -4

 replace worldbank_trust =. if worldbank_trust== -2 
 replace worldbank_trust =. if worldbank_trust== -3 
 replace worldbank_trust =. if worldbank_trust== -4
 
 
 sum wt  //country weights 
 
 keep country year mercosur_trust andeanpact_trust imf_trust worldbank_trust wt

 save Latinobarometro_2001.dta, replace
 
 *2002
 clear
 use Latinobarometro_2002_datos_eng_v2014_06_27.dta

 
 sum p55std p55ste p55stf
 
 br idenpa numinves p55sta

 rename numinves year
 rename idenpa country
 rename p55sta mercosur_trust
 rename p55std andeanpact_trust
 rename p55ste imf_trust
 rename p55stf worldbank_trust
 
   
 tab mercosur_trust 
 tab mercosur_trust ,nol
 tab andeanpact_trust , nol
 tab imf_trust, nol 
 tab worldbank_trust, nol
 
 replace mercosur_trust =. if mercosur_trust== -2 
 replace mercosur_trust =. if mercosur_trust== -4 
 replace mercosur_trust =. if mercosur_trust== -6
 
 replace andeanpact_trust =. if andeanpact_trust== -2 
 replace andeanpact_trust =. if andeanpact_trust== -6 
 replace andeanpact_trust =. if andeanpact_trust== -4

 replace imf_trust =. if imf_trust== -2 
 replace imf_trust =. if imf_trust== -6 
 replace imf_trust =. if imf_trust== -4

 replace worldbank_trust =. if worldbank_trust== -2 
 replace worldbank_trust =. if worldbank_trust== -6 
 replace worldbank_trust =. if worldbank_trust== -4

  sum wt  //country weights 

 keep country year mercosur_trust andeanpact_trust imf_trust worldbank_trust wt

 save Latinobarometro_2002.dta, replace

 

 *2003
 clear
 use Latinobarometro_2003_datos_eng_v2014_06_27.dta

 br idenpa numinves p43sta 

 rename numinves year
 rename idenpa country
 rename p43sta mercosur_trust
 rename p43std andeanpact_trust
 rename p43ste imf_trust

    
 tab mercosur_trust 
 tab mercosur_trust ,nol
 tab andeanpact_trust , nol
 tab imf_trust, nol 
 
 replace mercosur_trust =. if mercosur_trust== -2 
 replace mercosur_trust =. if mercosur_trust== -4 
 replace mercosur_trust =. if mercosur_trust== -6
 
 replace andeanpact_trust =. if andeanpact_trust== -2 
 replace andeanpact_trust =. if andeanpact_trust== -6 
 replace andeanpact_trust =. if andeanpact_trust== -4

 replace imf_trust =. if imf_trust== -2 
 replace imf_trust =. if imf_trust== -6 
 replace imf_trust =. if imf_trust== -4
 
  sum wt  //country weights 

  keep country year mercosur_trust andeanpact_trust imf_trust wt

 save Latinobarometro_2003.dta, replace

 

 *2009
 clear
 use Latinobarometro_2009_datos_eng_v2014_06_27.dta

 br idenpa numinves p56st_eb

 rename numinves year
 rename idenpa country
 rename p56st_eb mercosur_trust
 rename p56st_gb andeanpact_trust
 rename p56st_ab imf_trust
 rename p56st_bb worldbank_trust
 
 
 tab mercosur_trust 
 tab mercosur_trust ,nol
 tab andeanpact_trust , nol
 tab imf_trust, nol 
 
 replace mercosur_trust =. if mercosur_trust== -3 
 replace andeanpact_trust =. if andeanpact_trust== -3 
 replace imf_trust =. if imf_trust== -3 
 
  sum wt  //country weights 

 keep country year mercosur_trust andeanpact_trust imf_trust worldbank_trust wt

 save Latinobarometro_2009.dta, replace

 
 
 *2010
 clear
 use Latinobarometro_2010_datos_eng_v2014_06_27.dta

 br idenpa numinves P46ST_AB P46ST_BB P46N_FB

 rename numinves year
 rename idenpa country
 rename P46N_FB andeanpact_trust
 rename P46ST_AB imf_trust
 rename P46ST_BB worldbank_trust
 
  
 tab worldbank_trust ,nol
 tab andeanpact_trust , nol
 tab imf_trust, nol 
 
 replace andeanpact_trust =. if andeanpact_trust== -3
 replace andeanpact_trust =. if andeanpact_trust== -4

 replace imf_trust =. if imf_trust== -3
 replace worldbank_trust =. if worldbank_trust== -3

 
  sum wt  //country weights 

  
  keep country year  andeanpact_trust imf_trust worldbank_trust wt

  tab year
  
 save Latinobarometro_2010.dta, replace

 
 

 *2013
 clear
 use Latinobarometro2013Eng.dta

 br idenpa numinves P51ST_AB
 
 rename numinves year
 rename idenpa country
 rename P51ST_AB mercosur_trust
 rename P51ST_CB andeanpact_trust
 
 replace year = 2013
 
 tab mercosur_trust ,nol
 tab andeanpact_trust , nol
 
 replace mercosur_trust =. if mercosur_trust== -3 
 replace mercosur_trust =. if mercosur_trust== -2 
 replace mercosur_trust =. if mercosur_trust== -1
 
 replace andeanpact_trust =. if andeanpact_trust== -4 
 replace andeanpact_trust =. if andeanpact_trust== -3 
 replace andeanpact_trust =. if andeanpact_trust== -2

  sum wt  //country weights 


 keep country year mercosur_trust andeanpact_trust wt

   tab year


 save Latinobarometro_2013.dta, replace
 
 
 *2015
 clear
 use Latinobarometro_2015_Eng.dta

 br idenpa numinves P37ST_AB

 rename numinves year
 rename idenpa country
 rename P37ST_AB mercosur_trust
 rename P37ST_JB worldbank_trust
 
 tab year
 replace year = 2015

 *drop if year ==18
 
 tab mercosur_trust, nol
 tab worldbank_trust, nol
 
 replace mercosur_trust =. if mercosur_trust== -4 
 replace mercosur_trust =. if mercosur_trust== -3 
 replace mercosur_trust =. if mercosur_trust== -2

 
 replace worldbank_trust =. if worldbank_trust== -4 
 replace worldbank_trust =. if worldbank_trust== -3 
 replace worldbank_trust =. if worldbank_trust== -2

 sum wt  //country weights 

 
  keep country year mercosur_trust  worldbank_trust wt


 save Latinobarometro_2015.dta, replace

 
 
 ********************
 *Append all datasets: to create one dataset with individual observations of Mercosur and Adean Pact for each country and year
 
 use Latinobarometro_2001.dta, clear
 count
 tab year 

 append using Latinobarometro_2002.dta
 count
 tab year 
 
 append using Latinobarometro_2003.dta
 count
 tab year 

 append using Latinobarometro_2009.dta
 count
 tab year 

 append using Latinobarometro_2010.dta
 count
 tab year 
 
 append using Latinobarometro_2013.dta
 count
 tab year 
 
 append using Latinobarometro_2015.dta
 count
 tab year 


 save Latinobarometro_final.dta, replace
 
 /*
 use Latinobarometro_final.dta, clear
 
 tab year
 tab country
 
 tostring country, gen(country_str)
 tab country_str
*/
 
 
 
 
 
